package edu.spsu.library;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import edu.spsu.utils.Util;

public class RDFHandler extends DefaultHandler{

	Feed _feed;
	RDFItem _item;
	Context context; 
	String _lastElementName = "";
	boolean bFoundChannel = false;
	
	enum State{
		 NONE,
		 RDF_LINK,
		 RDF_TITLE,
		 RDF_SUBTITLE,
		 RDF_GENRE,
		 RDF_NAMEPART,
		 RDF_PUBLISHER,
		 RDF_TOPIC,
		 RDF_DATEISSUED,
		 RDF_CLASS
	};
	
	int depth = 0;
	State currentstate  = State.NONE;
	
	/*
	 * Constructor 
	 */
	public RDFHandler(Context cont)
	{
		context = cont;
	}
	/*
	 * getFeed - this returns our feed when all of the parsing is complete
	 */
	public Feed getFeed()
	{
		return _feed;
	}
	
	/*
	 * getItem - this returns last item from our feed when all of the parsing is complete
	 */
	public RDFItem getItem()
	{
		return _item;
	}
	
	
	public void startDocument() throws SAXException
	{
		// initialize our RDFFeed object - this will hold our parsed contents
		_feed = new Feed();
		
		// initialize the RDFItem object - we will use this as a crutch to grab the info from the channel
		// because the channel and items have very similar entries..
		_item = new RDFItem();
	}
	
	public void endDocument() throws SAXException
	{
		
	}
	public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
	{		
		depth++;
		if (localName.equals("channel"))
		{
			currentstate  = State.NONE;
			return;
		}
		if (localName.equals("image"))
		{
			// record our feed data - we temporarily stored it in the item :)
			_feed.setTitle(_item.get_subTitle());
			_feed.setPubDate(_item.get_dateIssued());
		}
		
		if (localName.equals("item"))
		{
			// create a new item
			_item = new RDFItem();
			return;
		}

		if (localName.equals("link"))
		{
			currentstate  = State.RDF_LINK;
			return;
		}
		if (localName.equals("title"))
		{
			currentstate  = State.RDF_TITLE;
			return;
		}
		if (localName.equals("subTitle"))
		{
			currentstate  = State.RDF_SUBTITLE;
			return;
		}
		if (localName.equals("namePart"))
		{
			currentstate  = State.RDF_NAMEPART;
			return;
		}
		if (localName.equals("genre"))
		{
			currentstate  = State.RDF_GENRE;
			return;
		}
		if (localName.equals("publisher"))
		{
			currentstate  = State.RDF_PUBLISHER;
			return;
		}
		if (localName.equals("dateIssued"))
		{
			currentstate  = State.RDF_DATEISSUED;
			return;
		}
		if (localName.equals("topic"))
		{
			currentstate  = State.RDF_TOPIC;
			return;
		}
		if (localName.equals("classification"))
		{
			currentstate  = State.RDF_CLASS;
			return;
		}
		// if we don't explicitly handle the element, make sure we don't wind up erroneously 
		// storing a newline or other bogus data into one of our existing elements
		currentstate  = State.NONE;
	}
	
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException
	{
		depth--;
		if (localName.equals("item"))
		{
			// add our item to the list!
			_feed.addItem(_item);
			return;
		}
	}
	 
	public void characters(char ch[], int start, int length)
	{
		String theString = new String(ch,start,length);
		Util.report(context, "RDFHandler In LibraryActivity",currentstate +" characters[" + theString + "]", false);
		
		switch (currentstate)
		{
			case RDF_LINK:
				_item.setLink(theString);
				currentstate  = State.NONE;
				break;
			case RDF_TITLE:
				_item.setTitle(theString);
				currentstate  = State.NONE;
				break;
			case RDF_SUBTITLE:
				_item.set_subTitle(theString);
				currentstate  = State.NONE;
				break;
			case RDF_GENRE:
				_item.set_genre(theString);
				currentstate  = State.NONE;
				break;
			case RDF_NAMEPART:
				_item.set_namePart(theString);
				currentstate  = State.NONE;
				break;
			case RDF_PUBLISHER:
				_item.set_publisher(theString);
				currentstate  = State.NONE;
				break;
			case RDF_DATEISSUED:
				_item.set_dateIssued(theString);
				currentstate  = State.NONE;
				break;
			case RDF_TOPIC:
				_item.set_topic(theString);
				currentstate  = State.NONE;
				break;
			case RDF_CLASS:
				if(_item.get_classification() == null)
					_item.set_classification(theString);
				else
					_item.set_2ndclassification(theString);
				currentstate  = State.NONE;
				break;
			default:
				return;
		}
		
	}
}
